package com.jlu.config.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/*
自定义网关全局Filter
 */
@Configuration
public class CustomerGlobalFilter implements GlobalFilter,Ordered{

  /*
  类似于javaweb的doFilter
  exchange = request + response
   */
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    System.out.println("经过全局Filter处理");
    Mono<Void> filter = chain.filter(exchange);
    System.out.println("响应回来经过全局Filter");

    return filter;
  }
/*
  用来指定Filter的执行顺序 默认按自然顺序排序 -1表示最高级别
 */
  public int getOrder() {
    return 0;
  }
}
